﻿using CanDichVu.Db;
using CanDichVu.Service.Iservice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CanDichVu.Service.Vervices
{
    public class RoleSer : IRoles
    {
        private readonly EntitiesCanDichVu Context = new EntitiesCanDichVu();

        public int addOrupdate(Role entity)
        {
            try
            {
                if (entity.Id == 0) Context.Roles.Add(entity);
                else Context.Entry(entity).State = System.Data.EntityState.Modified;
                Context.SaveChanges();
                return 1;
            }
            catch { return 0; }
        }

        public void delete(int id)
        {
            try
            {
                var entity = Context.Roles.SingleOrDefault(o => o.Id == id);
                Context.Roles.Remove(entity);
                Context.SaveChanges();
            }
            catch { }
        }

        public void deleteByName(string Name)
        {
            try
            {
                var entity = Context.Roles.SingleOrDefault(o => o.Name == Name);
                Context.Roles.Remove(entity);
                Context.SaveChanges();
            }
            catch { }
        }

        public string[] GetRolesForUser(string userName)
        {
            var c = (from i in Context.Roles
                     join m in Context.UserInRoles on i.Id equals m.RolesId
                     join l in Context.ThanhViens on m.Id equals l.Id
                     where l.UserName == userName
                     select i).ToList();

            List<string> Temp = new List<string>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    Temp.Add(g.Name);
                }
            }

            return Temp.ToArray();
        }

        public Role GetById(int id)
        {
            try
            {
                return Context.Roles.SingleOrDefault(p => p.Id == id);
            }
            catch { return null; }
        }

        public bool IsUserInRole(string username, string roleName)
        {
            try
            {
                var c = (from i in Context.Roles
                         join m in Context.UserInRoles on i.Id equals m.RolesId
                         join l in Context.ThanhViens on m.Id equals l.Id
                         where l.UserName == username && i.Name == roleName
                         select l);

                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public List<Role> List()
        {
            try
            {
                return Context.Roles.ToList();
            }
            catch { return null; }
        }

        public int CreateOrUpdate_PhanQuyen(UserInRole entity)
        {
            try
            {
                if (entity.UiRId == 0) Context.UserInRoles.Add(entity);
                else Context.Entry(entity).State = System.Data.EntityState.Modified;
                Context.SaveChanges();
                return 1;
            }
            catch { return 0; }
        }

        public UserInRole GetPhanquyenByUser(int userid)
        {
            try
            {
                var c = from i in Context.ThanhViens
                        join k in Context.UserInRoles on i.Id equals k.Id
                        join l in Context.Roles on k.RolesId equals l.Id
                        where i.Id == userid
                        select k;

                return c.First();
            }
            catch { return null; }
        }
    }
}
